﻿using System.IO;
using System.Linq;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Dialect;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using OpenWaves.ActivityFeed.NHibernate;

namespace OpenWaves.ActivityFeed.UnitTests_NHibernate
{
    public class NHibernateSessionFactoryTests
    {
        [Test]
        [Ignore]
        public void Given_connectionString_nhibernate_exports_database_schema()
        {            
            const string outputFile = "schema.sql";

            if (File.Exists(outputFile)) File.Delete(outputFile);

            var sessionFactoryConfiguration = new NHibernateSessionFactoryConfiguration(Consts.ActivityFeedSqlConnectionStringName);
            var sessionFactory = new NHibernateSessionFactory(sessionFactoryConfiguration, false, 
                cfg => new SchemaExport(cfg).Execute(s => File.AppendAllText(outputFile, s), false, false)).CreateSessionFactory();            

            Assert.NotNull(sessionFactory);

            Assert.IsTrue(File.Exists(outputFile));

            var schemaContent = File.ReadAllText(outputFile);            

            Assert.IsNotEmpty(schemaContent);
        }

        [Test]
        [Ignore]
        public void Given_connectionString_updates_generator_fills_database_with_fake_data()
        {
            var sessionFactoryConfiguration = new NHibernateSessionFactoryConfiguration(Consts.ActivityFeedSqlConnectionStringName);
            var sessionFactory = new NHibernateSessionFactory(sessionFactoryConfiguration, true).CreateSessionFactory();
            using (var session = sessionFactory.OpenSession())
            {
                session.FlushMode = FlushMode.Commit;

                using (var transaction = session.BeginTransaction())
                {
                    var sessionWrapper = new NHibernateSessionWrapper(session);

                    var srcDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent;
                    var loremIpsumFileName = Path.Combine(srcDirectory.FullName, @"ActivityFeed.Web.Tests\loremIpsum.txt");                    

                    var activityFeedService = new NHibernateActivityFeedService(sessionWrapper);
                    var updatesGenerator = new UpdatesGenerator(loremIpsumFileName, activityFeedService);                    

                    var updateGeneratorOptions = new UpdatesGeneratorOptions() {NumberOfUpdates = 1000};
                    updateGeneratorOptions.Users.AddRange(new string[] { "John", "Edwin", "Matt" });
                    updateGeneratorOptions.Topics.AddRange(new Topic[] { new Topic("Topic1"), new Topic("Topic2"), new Topic("Topic3"), new Topic("Topic4"), new Topic("Topic5") });

                    updatesGenerator.GenerateUpdates(updateGeneratorOptions);
                    transaction.Commit();

                    var updates = activityFeedService.GetUpdates().ToList();

                    Assert.That(updates.Count > 0);    
                }                                
            }
        }
    }
}